package cn.edu.nks.dormitory.home.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import cn.edu.nks.dormitory.home.db.IDBService;
import cn.edu.nks.dormitory.home.dbimpl.DBService;
import cn.edu.nks.dormitory.model.ModuleInfo;
import cn.edu.nks.dormitory.model.ModulePower;
/**
 * 加载功能模块信息
 * @author wangbs
 *
 */
public class LoadModuleInfoAction extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(LoadModuleInfoAction.class);
	private IDBService idbService = new DBService();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("application/jsons;charset=utf-8");
		String roleId = req.getParameter("roleId");
		if(roleId == null || roleId.equals("")){
			logger.warn("role id is null");
			return;
		}
		JSONArray array = new JSONArray();
		List<ModulePower> modulePowers = idbService.getModulePowerByRoleId(Integer.valueOf(roleId));
		logger.warn(String.format("load module size %d", modulePowers.size()));
		for (ModulePower modulePower : modulePowers) {
			ModuleInfo moduleInfo = idbService.getModuleInfoByModuleId(modulePower.getModuleId());
			JSONObject obj = JSONObject.fromObject(moduleInfo);
			array.add(obj);
		}
		logger.warn(String.format("load module info :%s", array.toString()));
		PrintWriter out = resp.getWriter();
		out.write(array.toString());
	}
}
